package response

import (
	"bytes"
	"encoding/json"
	"github.com/labstack/echo/v4"
	"net/http"
)

type Data struct {
	Success bool            `json:"success"`
	Msg     string          `json:"msg"`
	Data    json.RawMessage `json:"data"`
}

func RespSuccess(ctx echo.Context, msg string, data interface{}) error {
	var buffer bytes.Buffer

	m := json.NewEncoder(&buffer)
	if err := m.Encode(data); err != nil {
		return RespErr(ctx, "转换Json失败")
	}

	return ctx.JSON(http.StatusOK, &Data{
		Success: true,
		Msg:     msg,
		Data:    buffer.Bytes(),
	})
}

func RespErr(ctx echo.Context, msg string) error {
	return ctx.JSON(http.StatusOK, &Data{
		Success: false,
		Msg:     msg,
		Data:    nil,
	})
}